package com.project.controller;


import com.project.dto.RiderDto;
import com.project.entity.Rider;
import com.project.mapper.TOrderMapper;
import com.project.mapper.TRiderMapper;
import com.project.service.IRiderRemoteService;
import com.project.service.IRiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangyang
 * @since 2021-10-06
 */
@RestController
@RequestMapping("/rider")
public class TRiderController {
    @Autowired
    private IRiderService riderService;
    @Autowired
    private TOrderMapper orderMapper;
    @Autowired
    private IRiderRemoteService service;
    @Autowired
    private TRiderMapper riderMapper;

//    @Scheduled(cron = "5 * * * * *")//每分钟的第五秒执行以下代码
//    public void add(){
//        List<RiderDto> dto = service.findAllByRemote();
//        List<Rider> riderList = new ArrayList<>();
//        for (RiderDto riderDto : dto) {
//            Rider rider = new Rider();
//            rider.setCode(riderDto.getCode());
//            rider.setName(riderDto.getName());
//            rider.setPhone(riderDto.getTel());
//            rider.setSex(riderDto.getSex());
//            rider.setTime(riderDto.getRegistrationTime());
//            riderList.add(rider);
//        }
//
//        List<Rider> byItem = findByItem(null, null, null, null);
//        A: for (int i = 0; i < riderList.size(); i++) {
//            String code = riderList.get(i).getCode();
//
//            for (int i1 = 0; i1 < byItem.size(); i1++) {
//                String code1 = byItem.get(i1).getCode();
//                if (code.equals(code1)){
//                    continue A;
//                }
//            }
//            riderMapper.insert(riderList.get(i));
//        }
//    }


    @GetMapping("/findByItem")
    public List<Rider> findByItem(String name, String sex, LocalDate startTime, LocalDate endTime) {
        List<Rider> byItem = riderService.findByItem(name, sex, startTime, endTime);
        for (int i = 0; i < byItem.size(); i++) {
            Rider rider = byItem.get(i);
            Rider rider1 = orderMapper.findRider(rider.getCode());
            rider.setOrderNum(rider1.getOrderNum());
            rider.setOrderMoney(rider1.getOrderMoney());
            byItem.set(i, rider);
        }
        return byItem;
    }

    @GetMapping("/findById")
    public Rider findById(String id) {
        return riderService.findById(id);
    }
}
